package com.pd.backend.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pd.backend.dtos.AllocationCourier;
import com.pd.backend.dtos.PageResponseResult;
import com.pd.backend.dtos.ResponseResult;
import com.pd.backend.dtos.TaskPickupDto;
import com.pd.backend.enums.AppHttpCodeEnum;
import com.pd.backend.mapper.PdAuthUserMapper;
import com.pd.backend.mapper.PdTaskPickupDispatchMapper;
import com.pd.backend.pojo.PdAuthUser;
import com.pd.backend.service.PdTaskPickupDispatchService;
import com.pd.backend.vo.TaskPickupDispatchVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * 快递作业管理业务层实现
 */
@Service
@Transactional
@Slf4j
public class PdTaskPickupDispatchServiceImpl implements PdTaskPickupDispatchService {
    @Autowired
    private PdTaskPickupDispatchMapper pdTaskPickupDispatchMapperp;
    @Autowired
    private PdAuthUserMapper pdAuthUserMapper;
    @Override
    public PageResponseResult page(TaskPickupDto taskPickupDto) {
        //校验参数
        if (Objects.isNull(taskPickupDto)){
            PageResponseResult pageResponseResult = new PageResponseResult();
            pageResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
            return pageResponseResult;
        }
        if (Objects.isNull(taskPickupDto.getPage())||Objects.isNull(taskPickupDto.getSize())){
            taskPickupDto.checkParam();
        }
        Page<TaskPickupDispatchVo> page = PageHelper.startPage(taskPickupDto.getPage(), taskPickupDto.getSize());
        TaskPickupDispatchVo taskPickupDispatchVo = pdTaskPickupDispatchMapperp.page(taskPickupDto);
        PageResponseResult pageResponseResult = new PageResponseResult();
        pageResponseResult.setData(taskPickupDispatchVo);
        pageResponseResult.setCurrentPage(page.getPageNum());
        pageResponseResult.setTotal((int) page.getTotal());
        pageResponseResult.setSize(page.getPageSize());
        return pageResponseResult;
    }

    /**
     * 分配快递员
     * @param allocationCourier
     * @return
     */
    @Override
    public ResponseResult allocationCourier(AllocationCourier allocationCourier) {
        //校验参数
        if (Objects.isNull(allocationCourier)
                || StringUtils.isBlank(allocationCourier.getId())
                || StringUtils.isBlank(allocationCourier.getCourierId())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //根据id查询快递员是否存在
        PdAuthUser pdAuthUser = pdAuthUserMapper.selectCourier(allocationCourier.getCourierId());
        if (Objects.isNull(pdAuthUser)){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //根据id修改快递员
        int result = pdTaskPickupDispatchMapperp.updateCourierById(allocationCourier);
        if (result < 1){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        return ResponseResult.okResult(null);
    }
}